//
//  PhoneRegisterViewController.m
//  MeterMart
//
//  Created by ENERGY on 2018/10/6.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "PhoneRegisterViewController.h"

static int i = 599;
@interface PhoneRegisterViewController ()<UITextFieldDelegate>
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong)UIView * headerView;
@property (nonatomic, strong)UITextField * phoneNumTextField;//电话号
@property (nonatomic, strong)UITextField * verificationCodeTextField;//验证码
@property (nonatomic, strong)UIButton * verificationCodeBtn;//验证码按钮
@property (nonatomic, strong)UITextField * inviteTextField;//邀请码
@property (nonatomic, strong)UIButton * boundBtn;//绑定按钮
@property (nonatomic, strong)NSTimer * timer;
@property (nonatomic, strong)NSString * phoneString;
@property (nonatomic, strong)NSString * verificationString;
- (void)test:(NSTimer *)timmer;
@end

@implementation PhoneRegisterViewController(networking)
//发送验证码
- (void)sendCodeMessage{
    NSDictionary * dic = @{
                           @"phone":self.phoneNumTextField.text,
                           @"unionid":[UserModel sharedInstanced].userID
                           };
    [self showprogressHUD];
    [[AFAppDotNetAPIClient sharedClient] POST:@"usApi/sendnum" parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"sign"] isEqualToString:@"1"]) {
            self.verificationCodeBtn.enabled = NO;
            //初始化定时器
            self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(test:) userInfo:nil repeats:YES];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}
//绑定手机
- (void)bindPhone{
    NSDictionary * dic = @{
                           @"phone":self.phoneNumTextField.text,
                           @"unionid":[UserModel sharedInstanced].userID,
                           @"code":self.verificationCodeTextField.text
                           };
    [self showprogressHUD];
    [[AFAppDotNetAPIClient sharedClient] POST:@"usApi/bindphone" parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"sign"] isEqualToString:@"1"]) {
            [UserModel sharedInstanced].mobile = self.phoneNumTextField.text;
//            [self.payDelegateMoneyView showView];
            if (self.dismissControllerBlock) {
                self.dismissControllerBlock();
            }
//            [self dismissViewControllerAnimated:YES completion:nil];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}
@end
@implementation PhoneRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"绑定手机号";
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    
    UIButton * backBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 24/WIDTH_6_SCALE, 30/WIDTH_6_SCALE)];
    [backBtn addTarget:self action:@selector(backBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [backBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    UIBarButtonItem * leftItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
}
#pragma mark - init
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - TABBAR_HEIGHT- BOTTOM_HEIGHT) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.tableHeaderView = self.headerView;
    }return _tableView;
}
- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100/WIDTH_6_SCALE)];
        //手机号
        self.phoneNumTextField = [[UITextField alloc] initWithFrame:CGRectMake(40/WIDTH_6_SCALE, 43/WIDTH_6_SCALE, SCREEN_WIDTH - 80/WIDTH_6_SCALE, 40/WIDTH_6_SCALE)];
        self.phoneNumTextField.delegate = self;
        self.phoneNumTextField.placeholder = @"请输入手机号";
        self.phoneNumTextField.font = DEF_FontSize_14;
        self.phoneNumTextField.leftViewMode = UITextFieldViewModeAlways;
        UIView * phoneNumPlaceHoldImgBG = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 39/WIDTH_6_SCALE, self.phoneNumTextField.height)];
        phoneNumPlaceHoldImgBG.backgroundColor = [UIColor clearColor];
        UIImageView *phoneNumImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15/WIDTH_6_SCALE, 13/WIDTH_6_SCALE, 11/WIDTH_6_SCALE, 16/WIDTH_6_SCALE)];
        phoneNumImageView.image = [UIImage imageNamed:@"boundPhone"];
        [phoneNumPlaceHoldImgBG addSubview:phoneNumImageView];
        self.phoneNumTextField.leftView = phoneNumPlaceHoldImgBG;
        self.phoneNumTextField.backgroundColor = getColor(mainColor);
        [_headerView addSubview:self.phoneNumTextField];
        //验证码
        self.verificationCodeTextField = [[UITextField alloc] initWithFrame:CGRectMake(self.phoneNumTextField.left, self.phoneNumTextField.bottom + 16/WIDTH_6_SCALE, 191/WIDTH_6_SCALE, self.phoneNumTextField.height)];
        self.verificationCodeTextField.delegate = self;
        self.verificationCodeTextField.placeholder = @"输入验证码";
        self.verificationCodeTextField.font = DEF_FontSize_14;
        self.verificationCodeTextField.backgroundColor = getColor(mainColor);
        self.verificationCodeTextField.leftViewMode = UITextFieldViewModeAlways;
        UIView * verificationCodePlaceHoldImgBG = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 39/WIDTH_6_SCALE, self.verificationCodeTextField.height)];
        verificationCodePlaceHoldImgBG.backgroundColor = [UIColor clearColor];
        UIImageView *verificationCodeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15/WIDTH_6_SCALE, 13/WIDTH_6_SCALE, 14/WIDTH_6_SCALE, 16/WIDTH_6_SCALE)];
        verificationCodeImageView.image = [UIImage imageNamed:@"lock"];
        [verificationCodePlaceHoldImgBG addSubview:verificationCodeImageView];
        self.verificationCodeTextField.leftView = verificationCodePlaceHoldImgBG;
        self.verificationCodeTextField.backgroundColor = getColor(mainColor);
        [_headerView addSubview:self.verificationCodeTextField];
        //验证码按钮
        self.verificationCodeBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.verificationCodeTextField.right + 10/WIDTH_6_SCALE, self.verificationCodeTextField.top, SCREEN_WIDTH - self.verificationCodeTextField.left * 2 - 10/WIDTH_6_SCALE - self.verificationCodeTextField.width, self.verificationCodeTextField.height)];
        [self.verificationCodeBtn setBackgroundColor:getColor(mainRedColor)];
        self.verificationCodeBtn.titleLabel.font = DEF_FontSize_14;
        [self.verificationCodeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.verificationCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [self.verificationCodeBtn addTarget:self action:@selector(verificationCodeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_headerView addSubview:self.verificationCodeBtn];
        //邀请码
        self.inviteTextField = [[UITextField alloc] initWithFrame:CGRectMake(self.phoneNumTextField.left, self.verificationCodeTextField.bottom + 16/WIDTH_6_SCALE, self.phoneNumTextField.width, self.phoneNumTextField.height)];
        self.inviteTextField.placeholder = @"输入邀请码";
        self.inviteTextField.backgroundColor = getColor(mainColor);
        self.inviteTextField.leftViewMode = UITextFieldViewModeAlways;
        UIView * invitePlaceHoldImgBG = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 39/WIDTH_6_SCALE, self.verificationCodeTextField.height)];
        invitePlaceHoldImgBG.backgroundColor = [UIColor clearColor];
        UIImageView *inviteImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15/WIDTH_6_SCALE, 13/WIDTH_6_SCALE, 14/WIDTH_6_SCALE, 16/WIDTH_6_SCALE)];
        inviteImageView.image = [UIImage imageNamed:@"lock"];
        [invitePlaceHoldImgBG addSubview:inviteImageView];
        self.inviteTextField.leftView = invitePlaceHoldImgBG;
        self.inviteTextField.backgroundColor = getColor(mainColor);
        [_headerView addSubview:self.inviteTextField];
        self.inviteTextField.hidden = YES;
        //绑定按钮
        self.boundBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.phoneNumTextField.left, self.inviteTextField.bottom + 38/WIDTH_6_SCALE, self.phoneNumTextField.width, 40/WIDTH_6_SCALE)];
        [self.boundBtn setBackgroundColor:getColor(mainColor)];
        [self.boundBtn setTitle:@"绑定" forState:UIControlStateNormal];
        self.boundBtn.titleLabel.font = DEF_FontSize_14;
        [self.boundBtn setTitleColor:getColor(@"898989") forState:UIControlStateNormal];
        [self.boundBtn addTarget:self action:@selector(boundBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_headerView addSubview:self.boundBtn];
        _headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.boundBtn.bottom + 20/WIDTH_6_SCALE);
    }return _headerView;
}
#pragma mark - action
- (void)backBtnClick:(UIButton *)sender{
    if (self.backBtnClickBlock) {
        self.backBtnClickBlock();
    }
}
- (void)verificationCodeBtnClick:(UIButton *)sender{
    if (self.phoneNumTextField.text.length == 0 || [self.phoneNumTextField.text isEqualToString:@""] || self.phoneNumTextField.text == nil) {
        ShowMessage(@"请输入手机号");
        return;
    }
    if ([self valiMobile:self.phoneNumTextField.text]) {
        [self.view endEditing:YES];
        [self sendCodeMessage];//发送验证码
    }else{
        ShowMessage(@"请输入正确的手机号");
    }
}
- (void)boundBtnClick:(UIButton *)sender{
    NSLog(@"绑定");
    if (self.phoneNumTextField.text.length == 0 || [self.phoneNumTextField.text isEqualToString:@""] || self.phoneNumTextField.text == nil) {
        ShowMessage(@"请输入手机号");
        return;
    }
    if ([self valiMobile:self.phoneNumTextField.text]) {
        if (self.verificationCodeTextField.text.length == 0 || [self.verificationCodeTextField.text isEqualToString:@""] || self.verificationCodeTextField.text == nil) {
            ShowMessage(@"请输入验证码");
        }else{
            [self bindPhone];
        }
    }else{
        ShowMessage(@"请输入正确的手机号");
    }
}
- (void)test:(NSTimer *)timer{
    if (i != 1) {
        i -= 1;
        self.verificationCodeBtn.enabled = NO;
        [self.verificationCodeBtn setTitle:[NSString stringWithFormat:@"剩余%i秒", i] forState:UIControlStateNormal];
    } else {
        
        self.verificationCodeBtn.enabled = YES;
        [self.verificationCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        i = 30;
        //停掉定时器
        [self.timer invalidate];
        self.timer = nil;
    }
}
#pragma mark - textFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (textField == self.phoneNumTextField) {
        self.phoneString = string;
    }
    if (textField == self.verificationCodeTextField) {
        self.verificationString = string;
    }
    if (self.phoneString.length && self.verificationString.length > 0) {
        [self.boundBtn setBackgroundColor:getColor(mainRedColor)];
        [self.boundBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.boundBtn.userInteractionEnabled = YES;
    }else{
        [self.boundBtn setBackgroundColor:getColor(mainColor)];
        [self.boundBtn setTitleColor:getColor(@"898989") forState:UIControlStateNormal];
        self.boundBtn.userInteractionEnabled = NO;
    }
    return YES;
}
@end
